package com.mdz.ypxspt.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.ypxspt.Entity.SysSpkfk;
import com.mdz.ypxspt.Mapper.SysSpkfkMapper;
import com.mdz.ypxspt.Service.Impl.SysSpkfkServiceImpl;
import com.mdz.ypxspt.Utils.FileUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2019-12-15
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Controller
@RequestMapping("/ypxspt/sysSpkfk")
public class SysSpkfkController {
    @Autowired
    private SysSpkfkMapper spkfkMapper;

    @ResponseBody
    @RequestMapping(value = "/insertspkfk", method = RequestMethod.POST)
    public int insertspkfk(SysSpkfk sysSpkfk) {
        Integer i = spkfkMapper.insert(sysSpkfk);
        return i;
    }

    @ResponseBody
    @RequestMapping(value = "/editspkfk/{spid}", method = RequestMethod.PUT)
    public Integer editspkfk(SysSpkfk sysSpkfk) {
        System.out.println(sysSpkfk);
        return spkfkMapper.update(sysSpkfk, new UpdateWrapper<SysSpkfk>().eq("spid", sysSpkfk.getSpid()));
    }

    @ResponseBody
    @RequestMapping(value = "/getspkfk/{spid}", method = RequestMethod.GET)
    public SysSpkfk getspkfk(@PathVariable("spid") String spid, ModelMap map) {
        SysSpkfk sysSpkfk = spkfkMapper.selectOne(new QueryWrapper<SysSpkfk>().eq("spid", spid));
        map.put("spkfk", sysSpkfk);
        return sysSpkfk;
    }

    @ResponseBody
    @RequestMapping(value = "/deletespkfk/{spid}", method = RequestMethod.DELETE)
    public Integer deletespkfk(@PathVariable("spid") String spid) {
        return spkfkMapper.delete(new QueryWrapper<SysSpkfk>().eq("spid", spid));
    }

    @RequestMapping("/getspkfkbyspmch")
    public String getspkfkbyspmch(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "spmch", defaultValue = "") String spmch, ModelMap map) {
        PageHelper.startPage(pn, 20);
        List<SysSpkfk> spkfkList = spkfkMapper.selectList(new QueryWrapper<SysSpkfk>().like("spmch", spmch));
        PageInfo page = new PageInfo(spkfkList, 5);
        map.put("pageInfo", page);
        return "/success";
    }

    @ResponseBody
    @PostMapping("/pageInfospkfk")
    public List<SysSpkfk> pageInfospkfk(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 10);
        List<SysSpkfk> spkfkList = spkfkMapper.selectList(null);
        PageInfo page=new PageInfo(spkfkList,5);
        return spkfkList;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteallspkfk/{spids}", method = RequestMethod.DELETE)
    public Integer deleteallspkfk(@PathVariable("spids") String spids) {
        List<String> del_ids = new ArrayList<>();
        String[] str_spids = spids.split("-");
        for (String string : str_spids) {
            del_ids.add(string);
        }
        return spkfkMapper.delete(new QueryWrapper<SysSpkfk>().in("spid", del_ids));
    }

    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        List<SysSpkfk> spkfkList = spkfkMapper.selectList(null);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("商品资料表");
        sheet.setDefaultColumnWidth((short) 18);
        HSSFRow row = sheet.createRow(0);
        String[] head = {"spid", "spbh", "sptm", "spmch", "zjm", "beactive", "yishj", "is_gdsj", "is_gdzk", "is_tjsp", "is_hysp", "is_gmp", "is_gsp", "is_sy", "is_yp", "jingd", "denglrq", "gengxshj", "delrq", "dw", "shpchd", "shpgg", "kemuhao", "oldcode", "rkhw", "ckhw", "bzqfs", "fenlbh", "guojbh", "changjbh", "huaxm", "changym", "suming", "xiwname", "tongym", "shengccj", "pizhwh", "zhucsb", "jixing", "shiyzh", "bulfy", "chuczysx", "chuffl", "yaofpd", "yongfyyl", "jinjzh", "zhuysx", "yaowchf", "youxq", "shangplx", "guizbz", "yongyfl", "leibie", "jlgg", "otd", "ordercycle", "stkquot", "shlv", "hshjj", "jj", "shj", "hshsj", "lshj", "koul", "kcsx", "kcxx", "helkc", "bzqts", "yujts", "zgshl", "zhdcbj", "zdshj", "zgshj", "zgjjxz", "maolv", "huiytj", "itemlength", "itemheight", "itemwidth", "weight", "casevolume", "caseweight", "zuixdwtj", "zuixdwzl", "pfpj", "dispcolor", "jx", "bzgg", "lastmodifytime", "is_cmd", "lsgg", "is_zdyh", "is_jkyp", "minshl", "cx_dj_kl", "is_cx_dj", "zdyh", "zdhyzklshj", "is_cyhydz", "huiyshl", "nocalcopys", "fuzr", "is_jsh", "is_lc", "is_zhongyao", "type_bh", "zdlshj", "guanjzjh", "shiyq", "abbrImageURL", "cbflbh", "imageUrl", "is_ybsp", "yb_zfbl", "yb_spbh", "username", "chuchj", "pifj", "zhilbz", "chargelb", "is_sendybzx", "tj_ylshj", "is_hj", "is_yxbz", "wdid", "chbjs", "dwbh", "zengp", "beizhu", "yibao", "minbz", "is_pfuse", "oldspbh", "notmodif", "zhongbzshl", "jianbzshl", "gaim", "getpihao", "is_tb", "nocyzdkoul", "chdzjm", "qhfl", "colorbh", "yongyyf", "meiryl", "ismzl", "iszzts", "tuijdj", "ticje", "fenleibh", "wjybh", "shplx", "xsezkfl", "lsd", "jiyao", "zbjg", "qhbz", "kqfl", "pifati", "kpytc", "gsmdj", "sygsj", "xsptj", "xjyyj", "shydj", "lshpj", "jwh", "shuilv", "pizhwhyxq", "scgys", "dzjg", "baozhh", "shmsh", "zlbzxq", "dabh", "zldayj", "pzbs", "wtr", "wtsxq", "wj", "shjbg", "wzhs", "tongymzjm", "is_zhbzh", "is_zbz", "zbz", "end_rq", "is_zbpz", "pzgys", "pzlb", "zdzdshj", "mlpz", "zlzk", "dllx", "chpdljb", "nojiyao", "dijia", "zhuzlx", "louch", "is_ddkp", "zhongbj", "scsdw", "kufxx", "jingylb", "jinyyy", "jinyrq", "scyongyfl", "yanghnr", "yanghzd", "shuomsrq", "is_ycgy", "shchlx", "yhlb", "ccfl", "is_wt", "pjxq", "zglshj", "bcfl", "isyc", "isec", "xbzbqy", "isjkcy", "iskkpz", "iszd", "khfl", "jyzbqy", "is_nb", "xbzbj", "yblx", "is_xjjbyy", "is2012xb", "is520jy", "elgl", "is_zhxpz", "kel", "is_bg", "wendshx", "wendxx", "wtsccj", "zhaosj", "zhaobj", "is_jy", "shenhe", "shenhr", "shenhyj", "shenhrq", "is_ecys", "is_teshyp", "jxq", "is_yk", "qxxkzh", "hfcxspid", "fxcxspid", "is_pp", "swhbbm"};
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        for (short i = 0; i < head.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(head[i]);
            cell.setCellStyle(cellStyle);
        }
        for (int iBody = 0; iBody < spkfkList.size(); iBody++) {
            row = sheet.createRow(iBody + 1);
            SysSpkfk u = spkfkList.get(iBody);
            String[] spkfkArray = new String[4];
            spkfkArray[0] = u.getSpid();
            spkfkArray[1] = u.getSpbh();
            spkfkArray[2] = u.getSpmch();
            spkfkArray[3] = u.getShlv();
            for (int iArray = 0; iArray < spkfkArray.length; iArray++) {
                row.createCell(iArray).setCellValue(spkfkArray[iArray]);
            }
        }
        FileUtil.createFile(response, workbook);
    }
}

